package com.yinp.showview.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.yinp.showview.AppUtils;
import com.yinp.showview.R;

/**
 * @author :yinpeng
 * date      :2021/9/17
 * package   :com.yinp.proapp.module.customview.view
 * describe  :
 */
public class CircleLoopView extends View {
    public CircleLoopView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public CircleLoopView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private Paint loopPaint, inPaint;
    private int loopWidth = 8;
    private int loopColor;
    private int[] otherColor;
    private int[] otherStage;
    private int maxProcess = 100;
    private int curProcess = 50;
    private int duration = 1000;
    private int width;
    private int height;
    private RectF outRectF, inRectF;

    private void init(AttributeSet attrs) {

        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.CircleLoopView);

        loopColor = ta.getColor(R.styleable.CircleLoopView_clv_color, ContextCompat.getColor(getContext(), R.color.ff4d4d));
        loopWidth = ta.getInteger(R.styleable.CircleLoopView_clv_loop_width, loopWidth);
        maxProcess = ta.getInteger(R.styleable.CircleLoopView_clv_max_process, maxProcess);
        curProcess = ta.getInteger(R.styleable.CircleLoopView_clv_cur_process, curProcess);

        ta.recycle();

        loopWidth = AppUtils.dpToPx(getContext(), loopWidth);
        loopPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        loopPaint.setStyle(Paint.Style.STROKE);
        loopPaint.setStrokeWidth(loopWidth);
        loopPaint.setColor(loopColor);

        inPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        inPaint.setStrokeWidth(4);
        inPaint.setColor(Color.BLUE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
        inRectF = new RectF(loopWidth, loopWidth, width - loopWidth, height - loopWidth);
        outRectF = new RectF(loopWidth / 2, loopWidth / 2, width - loopWidth / 2, height - loopWidth / 2);
    }

    private int start;

    public void startAnimator(int start, final int end, long animTime, final TextView textView) {
        this.start = start;
        //属性动画
        ValueAnimator valueAnimator = ValueAnimator.ofInt(start, end);
        valueAnimator.setDuration(animTime);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int i = Integer.parseInt(String.valueOf(animation.getAnimatedValue()));
                Log.d("abcd", "onAnimationUpdate: " + i);
                if (textView != null) {
                    textView.setText(i + "");
                }
                curProcess = (int) (360 * (i * 1.0f / end));
                Log.d("abcd", "onAnimationUpdate: " + curProcess);
                postInvalidate();//刷新view
            }
        });
        valueAnimator.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (otherColor == null || otherColor.length == 0) {
            loopPaint.setColor(loopColor);
            drawLoop(canvas);
        } else {
            drawOther(canvas);
        }
    }

    private void drawLoop(Canvas canvas) {
        canvas.drawArc(outRectF, 0, curProcess, false, loopPaint);
    }

    private void drawOther(Canvas canvas) {
        int start = -90;
        for (int i = 0; i < otherColor.length; i++) {
            loopPaint.setColor(otherColor[i]);
            canvas.drawArc(outRectF, start, otherStage[i], false, loopPaint);
            start += otherStage[i];
            Log.d("abcd", "drawOther: " + start);
            Log.d("abcd", "drawOther:2 " + otherColor[i]);
        }
    }

    public void setOtherColorStage(int[] otherColor, int[] otherStage, float maxProcess) {
        for (int i = 0; i < otherColor.length; i++) {
            otherColor[i] = ContextCompat.getColor(getContext(), otherColor[i]);
        }
        this.otherColor = otherColor;
        for (int i = 0; i < otherStage.length; i++) {
            otherStage[i] = (int) (360 * (otherStage[i] / maxProcess));
        }
        this.otherStage = otherStage;
        postInvalidate();
    }

    public void setMaxProcess(int maxProcess) {
        this.maxProcess = maxProcess;
    }

    public void setCurProcess(int curProcess) {
        this.curProcess = curProcess;
    }
}
